package com.originskyseed.platform.user.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.user.i18n.UserResponseMsg;
import com.originskyseed.platform.user.model.PlayerRelateLabel;
import com.originskyseed.platform.user.model.PlayerRelateLabelFile;
import com.originskyseed.platform.user.model.UserInfo;
import com.originskyseed.platform.user.service.IPlayerRelateLabelFileService;
import com.originskyseed.platform.user.service.IPlayerRelateLabelService;
import com.originskyseed.platform.user.vo.request.PlayerRelateLabelRequestVO;

/**
 * 玩家标签表Controller类（OSI_PLAYER_RELATE_LABEL）
 *
 * @author wlj
 */
@Controller
@RequestMapping("/playerRelateLabel")
@BaseLocaleResource(localeFileName = "i18n_user")
public class PlayerRelateLabelController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(PlayerRelateLabelController.class);

	@DubboReference
	private IPlayerRelateLabelService playerRelateLabelService;

	@DubboReference
	private IPlayerRelateLabelFileService playerRelateLabelFileService;

	/**
	 * 分页查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/playerRelateLabelList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<PlayerRelateLabel>> playerRelateLabelList(HttpServletRequest request) {
		BaseResponseBody<BasePage<PlayerRelateLabel>> result = null;
		try {
			// 请求对象
			PlayerRelateLabelRequestVO requestVO = this.getParameterBean(PlayerRelateLabelRequestVO.class);

			// 分页查询
			BasePage<PlayerRelateLabel> page = new BasePage<PlayerRelateLabel>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.playerRelateLabelService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<PlayerRelateLabel>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<PlayerRelateLabel>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息_新增玩家标签
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/playerRelateLabelSave.json", method = RequestMethod.POST)
	@RequiredParams(names = { "playerId" })
	public BaseResponseBody<PlayerRelateLabel> playerRelateLabelSave(HttpServletRequest request) {
		BaseResponseBody<PlayerRelateLabel> result = null;
		try {
			// 请求对象
			PlayerRelateLabel playerRelateLabel = this.getParameterBean(PlayerRelateLabel.class);
			List<PlayerRelateLabelFile> playerRelateLabelFileList = this.getParameterList("playerRelateLabelFileList", PlayerRelateLabelFile.class);
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			if (StringUtils.isNotBlank(playerRelateLabel.getId())) {
				this.playerRelateLabelService.updateByPrimaryKey(playerRelateLabel);
			} else {
				this.playerRelateLabelService.insertSelective(playerRelateLabel, playerRelateLabelFileList, userInfo);
			}
			result = new BaseResponseBody<PlayerRelateLabel>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.LABEL_SAVE_SUCCESS.getMsg()), playerRelateLabel);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<PlayerRelateLabel>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.LABEL_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/playerRelateLabelDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> playerRelateLabelDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});

			this.playerRelateLabelService.deleteByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 查看玩家标签
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectPlayerRelateLabelByPlayerId.json")
	@RequiredParams(names = { "id" }) // 用户ID（玩家）
	public BaseResponseBody selectPlayerRelateLabelByPlayerId(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			String id = this.getParameterString("id", null);
			PlayerRelateLabel relateLabel = new PlayerRelateLabel();
			relateLabel.setPlayerId(id);
			List<PlayerRelateLabel> relateLabelList = this.playerRelateLabelService.selectList(relateLabel);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), relateLabelList);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

}
